package tech.aistar.controller.index;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tech.aistar.entity.Post;
import tech.aistar.service.IPostService;
import tech.aistar.service.IUserInfoService;
import tech.aistar.service.component.RankListComponent;
import tech.aistar.entity.index.RankDO;
import tech.aistar.service.component.RedisComponent;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/top")
public class IndexController {

    @Autowired
    private RankListComponent rankListComponent;
    @Autowired
    private IPostService postService;
    @Autowired
    private RedisComponent redisComponent;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserInfoService userInfoService;

    private static final String RANK_PREFIX = "global_rank";

    @GetMapping(path = "/topn")
    public String showTopN(Model model) {
        Set<String> keys = redisTemplate.keys("*");
        redisTemplate.delete(keys);
//        List<RankDO> tops = redisTemplate.opsForList().range("topten", 0, -1);
        List<Post> tops = redisTemplate.opsForList().range("topten", 0, -1);
        if (tops.size() == 0) {//如果缓存中的数据为空的时候进行重新获取数据
            List<Post> list = postService.findAllPost1();
            for (Post l : list) {
                redisComponent.add(RANK_PREFIX, String.valueOf(l.getPostId()), -l.getPointNum());
            }
//        }
            List<RankDO> top = rankListComponent.getTopNRanks(11);
            //放入
//            redisTemplate.opsForList().rightPushAll("topten", tops);
//            List<Post> toplist = new ArrayList<>();
            for (RankDO r : top) {
                Long a = r.getUserId();
                int i = a.intValue();
                Post p = postService.findPostByPostId(i);
                tops.add(p);
            }
            redisTemplate.opsForList().rightPushAll("topten", tops);
        }
        model.addAttribute("toplist",tops);
//        return toplist;
//        return rankListComponent.getTopNRanks(n);
        return "/index";
    }

    @GetMapping(path = "/topns")
    public @ResponseBody List<Post> showTopNs(Model model) {
//        List<RankDO> tops = redisTemplate.opsForList().range("topten", 0, -1);
        List<Post> tops = redisTemplate.opsForList().range("topten", 0, -1);
        return tops;
    }

//    @GetMapping(path = "/topns")
//    public @ResponseBody List<RankDO> showTopNs(Model model) {
//        //List<Post> lists = redisTemplate.opsForList().range("post", 0, -1);
//        //if (lists.size() == 0) {//如果缓存中的数据为空的时候进行重新获取数据
//        List<Post>list = postService.findAllPost1();
//        for (Post l : list) {
//            redisComponent.add(RANK_PREFIX, String.valueOf(l.getPostId()), -l.getPointNum());
//        }
////        }
//        List<Post> toplist = new ArrayList<>();
//        List<RankDO> top = rankListComponent.getTopNRanks(11);
////        for (RankDO r:top){
////            Long a = r.getUserId();
////            int i = a.intValue();
////            Post p = postService.findPostByPostId(i);
////            toplist.add(p);
////        }
//        model.addAttribute("toplist",toplist);
////        return toplist;
//        return rankListComponent.getTopNRanks(11);
////        return "/index";
//    }

//    @GetMapping(path = "/topns")
//    public @ResponseBody List<RankDO> showTopNs(Model model) {
//        List<RankDO> tops = redisTemplate.opsForList().range("topten", 0, -1);
//        if (tops.size() == 0) {//如果缓存中的数据为空的时候进行重新获取数据
//            List<Post> list = postService.findAllPost1();
//            for (Post l : list) {
//                redisComponent.add(RANK_PREFIX, String.valueOf(l.getPostId()), -l.getPointNum());
//            }
////        }
//            tops = rankListComponent.getTopNRanks(11);
//            //放入
//            redisTemplate.opsForList().rightPushAll("topten", tops);
//        }
//        return tops;
//    }

//    @GetMapping(path = "/topns")
//    public @ResponseBody List<Post> showTopNss() {
////        List<RankDO> tops = redisTemplate.opsForList().range("topten", 0, -1);
//        Set<String> keys = redisTemplate.keys("*");
//        redisTemplate.delete(keys);
//        List<Post> tops = redisTemplate.opsForList().range("topten", 0, -1);
//        if (tops.size() == 0) {//如果缓存中的数据为空的时候进行重新获取数据
//            List<Post> list = postService.findAllPost1();
//            for (Post l : list) {
//                redisComponent.add(RANK_PREFIX, String.valueOf(l.getPostId()), -l.getPointNum());
//            }
////        }
//            List<RankDO> top = rankListComponent.getTopNRanks(11);
//            //放入
////            redisTemplate.opsForList().rightPushAll("topten", tops);
////            List<Post> toplist = new ArrayList<>();
//            for (RankDO r : top) {
//                Long a = r.getUserId();
//                int i = a.intValue();
//                Post p = postService.findPostByPostId(i);
//                tops.add(p);
//            }
//            redisTemplate.opsForList().rightPushAll("topten", tops);
//        }
////        model.addAttribute("toplist",tops);
////        return toplist;
////        return rankListComponent.getTopNRanks(n);
//        return tops;
//    }



    @GetMapping(path = "/update")
    public @ResponseBody RankDO updateScores(long userId, float score) {
        return rankListComponent.updateRank(userId, score);
    }

    @PostMapping(path = "/update/{postId}/{pointNum}")
    public void updateScore(@PathVariable("postId") long postId,@PathVariable("pointNum") float pointNum) {
//        System.out.println("uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu");
//        rankListComponent.updateRank(postId, pointNum);
        Long a = postId;
        int i = a.intValue();
        Float p = pointNum;
        int j = p.intValue();
        userInfoService.updatePost(j,i);
//        Set<String> keys = redisTemplate.keys("*");
//        redisTemplate.delete(keys);
    }

    @GetMapping(path = "/rank")
    public RankDO queryRank(long userId) {
        return rankListComponent.getRank(userId);
    }

    @GetMapping(path = "/around")
    public List<RankDO> around(long userId, int n) {
        return rankListComponent.getRankAroundUser(userId, n);
    }

}
